home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Lightspeed C SKEL fldr / Skel nextevent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-17  |  4.0 KB  |  120 lines  |  [TEXT/KAHL]

  1. #include <WindowMgr.h>        
  2. #include <MenuMgr.h>
  3. #include <EventMgr.h>
  4. #include "Skel defines.h"
  5. #include "Skel globals.h"
  6.  
  7. #include <EventMgr.h>
  8.  
  9. /* 
  10. the main loop that handles events
  11. ############################   MainEventLoop  ##########################
  12.   
  13.   Brace yourself: here's where the action is.  Most Mac programs just
  14.   wait for events (as do we all), and then process them.  There are
  15.   two sorts of events: those directly initiated by the user, like key
  16.   presses and mouse-downs, and those consequent events posted by the
  17.   Event Manager, like update and activate events.  The latter MUST be
  18.   handled correctly and carefully.  In particular, it's important for
  19.   all events to make sure that the event occurred in or for the window
  20.   you expect -- it's possible to get events which are not for one of
  21.   our windows, despite GetNextEvent's return value.  Similarly,
  22.   be sure to check that the window it occured in is the active one,
  23.   if it matters.
  24.   
  25.   A common mistake in handling update and activate events is in finding
  26.   out which window they are for.  It is "(WindowPtr)myevent.message "
  27.   that gives this information, NOT "whichwindow" (the WindowPtr returned
  28.   by FindWindow).  The latter pointer merely tells you what window
  29.   the mouse was in at the time the event was posted -- completely
  30.   irrelevant for Update and Activate events.  Think it through carefully.
  31.   
  32. */
  33.  
  34. maineventloop () {
  35.  
  36. /* body of MainEventLoop */
  37.  
  38.     FlushEvents (everyEvent, 0);/* discard leftover events */
  39.  
  40. /* get next event, and Handle it appropriately, until user QUITs */
  41.  
  42.     userdone = 0;
  43.     do {
  44.     SystemTask ();        /* Handle desk accessories */
  45.     if (GetNextEvent (everyEvent, &myevent)) {
  46.                 /* get event; if for us... */
  47.         switch (myevent.what) {/* Handle each kind of event */
  48.         case mouseDown: /* find out what window the mouse went
  49.                    down in, and where in it */
  50.             windowcode = FindWindow (myevent.where, &whichwindow);
  51.             switch (windowcode) {
  52.                 /* Handle mouse-down for each place */
  53.             case inSysWindow: 
  54.                 /* Handle the desk accessories */
  55.                 SystemClick (&myevent, whichwindow);
  56.                 break;/* inSysWindow */
  57.             case inMenuBar: /* Handle the command */
  58.                 userdone = docommand (MenuSelect (myevent.where));
  59.                 break;/* inMenuBar */
  60.             case inDrag: /* drag the window */
  61.                 DragWindow (whichwindow, myevent.where, &dragrect);
  62.                 break;
  63.             case inContent: 
  64.                 /* includes inGrow if window inactive.
  65.                    Activate window */
  66.                 if (whichwindow == mywindow)
  67.                 /* make sure it's for mine */
  68.                 if (whichwindow != FrontWindow ())
  69.                     SelectWindow (whichwindow);
  70.                 /* make it active */
  71.                 break;
  72.             case inGrow: 
  73.                 /* window is already active; change its
  74.                    size */
  75.                 if (whichwindow == mywindow)
  76.                 /* moke sure it's for mine */
  77.                 resize (mywindow, myevent.where);
  78.                 break;/* inGrow */
  79.             case inGoAway: 
  80.                 /* we don't have a GoAway Region */
  81.             /* break;  I got a compiler error here with the
  82.                break left in.  Good C programming Style would
  83.                have a break even after the last case - WHJ
  84.                3/11/85 */
  85.             break; /* put it in for LSC */
  86.             }
  87.             break;    /* switch */
  88.  
  89.         case keyDown: 
  90.         case autoKey:     /* if command key, pass the char to
  91.                    MenuKey */
  92.             if ((myevent.modifiers & cmdKey) != 0)
  93.             userdone = docommand (MenuKey ((char) (myevent.message & charcodemask)));
  94.         case updateEvt: /* if it's for our window, update it */
  95.             if ((WindowPtr) (myevent.message) == mywindow)
  96.             updatewindow (mywindow);
  97.                 /* redraw the window contents */
  98.             break;
  99.         case activateEvt: 
  100.                 /* if for our window, set port as nec. */
  101.             if ((WindowPtr) (myevent.message) == mywindow) {
  102.                 /* my window */
  103.             DrawGrowIcon (mywindow);
  104.                 /* redraw grow icon to reflect new state 
  105.                 */
  106.             if (myevent.modifiers & 1)
  107.                 /* odd means an activate event */
  108.                 SetPort (mywindow);
  109.                 /* activate evt: work in our own port */
  110.             else
  111.                 SetPort (screenport);
  112.                 /* deactivate evt: our port is gone; keep
  113.                    port from dangling */
  114.             } break;
  115.         }
  116.     }
  117.  
  118.     } while (userdone == 0);
  119. }
  120.